import 'dart:io';
import 'package:camera_camera/page/camera.dart';
import 'package:dio/dio.dart';
import 'package:dustbin_sort/model/res_type_entity.dart';
import 'package:dustbin_sort/widget/message_dialog.dart';
import 'package:dustbin_sort/widget/progress_dialog.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:camera/camera.dart';
import 'package:fluttertoast/fluttertoast.dart';



class PhotoPage extends StatefulWidget {
	@override
	_PhotoPageState createState() => _PhotoPageState();
}

class _PhotoPageState extends State<PhotoPage> {
	CameraController controller;
	ProgressDialog progressDialog;
	File val;
	bool _loading = false;

	final PageController _controller = PageController(
		initialPage: 2,
	);


	Widget _resLayout()
	{
		return ProgressDialog(child: Container(
				child: ListView(
					children: <Widget>[
						ClipRRect(
							borderRadius: BorderRadius.circular(10),
							child: new Image.file(
								val,
								fit: BoxFit.cover,
							),
						),
						Container(
							child: MaterialButton(
								color: Colors.blue,
								textColor: Colors.white,
								child: new Text('开始识别'),
								onPressed: () {
									// ...
									_upLoadImage(val);
								},
							),
						)
					],
				)
		),loading: _loading,);

	}

	@override
	Widget build(BuildContext context) {
		return Scaffold(
				appBar: AppBar(
					title: Text("选择照片",style: TextStyle(color: Colors.black),),
					backgroundColor: Colors.white,
					elevation: 0,

				),
				floatingActionButton: FloatingActionButton(
						child: Icon(Icons.camera_alt, ),
						onPressed: () async {
							val = await showDialog(
									context: context,
									builder: (context) => Camera(
										mode: CameraMode.fullscreen,
										orientationEnablePhoto: CameraOrientation.landscape,
									)
							);

							print(val.path);
							setState(() {});
						}
						),
				body: Container(

						child: Container(
								padding: EdgeInsets.all(8),child: val != null
							?
						_resLayout()

								:
						Container(
									height: MediaQuery.of(context).size.height,
									child: ClipRRect(
										borderRadius: BorderRadius.circular(10),
										child: new Image.asset(
											"assets/images/plant_1.jpg",
											fit: BoxFit.cover,
										),
									),
								),

						)
				)
		);
	}

	//上传图片
	_upLoadImage(File image) async {
		setState(() {
		  _loading = !_loading;
		});
		String path = image.path;
		var name = path.substring(path.lastIndexOf("/") + 1, path.length);
		var suffix = name.substring(name.lastIndexOf(".") + 1, name.length);
		FormData formData = new FormData.fromMap({
			"userId": "10000024",
			"file": await MultipartFile.fromFile(path, filename: name)
		});

		Dio dio = new Dio();
		var response = await dio.post("http://www.rainfalse.com:8081/upload", data: formData);
		setState(() {
			_loading = !_loading;
		});
		if (response.statusCode == 200) {
			ResTypeEntity resTypeEntity = ResTypeEntity().fromJson(response.data);
			print(resTypeEntity.type);
			if(resTypeEntity.type == 0){
				showDialog(context: context,barrierDismissible: true,
						builder: (_){
							return MessageDialog(
								imageAsset: "assets/images/recycle_icon.png",
								title: "分类信息",
								content: Container(
									alignment: Alignment.center,
									padding: EdgeInsets.only(left: 10,right: 10),
									child: Text("属于可回收垃圾", style: TextStyle(fontSize: 17, color: Color.fromARGB(255, 75, 75, 75)),),
									decoration: BoxDecoration(
										borderRadius: BorderRadius.all(Radius.circular(10)),
										color: Color.fromARGB(255, 240, 240, 240),
									),
								),
								positiveText: "好的",
								onPositivePressEvent: (){
									Navigator.pop(context);
								},
								onCloseEvent: (){
									Navigator.pop(context);
								},
							);
						}
				);
			}else if(resTypeEntity.type == 1){
				showDialog(context: context,barrierDismissible: true,
						builder: (_){
							return MessageDialog(
								imageAsset: "assets/images/dry_icon.png",
								title: "分类信息",
								content: Container(
									alignment: Alignment.center,
									padding: EdgeInsets.only(left: 10,right: 10),
									child: Text("属于干垃圾", style: TextStyle(fontSize: 17, color: Color.fromARGB(255, 75, 75, 75)),),
									decoration: BoxDecoration(
										borderRadius: BorderRadius.all(Radius.circular(10)),
										color: Color.fromARGB(255, 240, 240, 240),
									),
								),
								positiveText: "好的",
								onPositivePressEvent: (){
									Navigator.pop(context);
								},
								onCloseEvent: (){
									Navigator.pop(context);
								},
							);
						}
				);
			}else if(resTypeEntity.type == 2){
				showDialog(context: context,barrierDismissible: true,
						builder: (_){
							return MessageDialog(
								imageAsset: "assets/images/danger_icon.png",
								title: "分类信息",
								content: Container(
									alignment: Alignment.center,
									padding: EdgeInsets.only(left: 10,right: 10),
									child: Text("属于有害垃圾", style: TextStyle(fontSize: 17, color: Color.fromARGB(255, 75, 75, 75)),),
									decoration: BoxDecoration(
										borderRadius: BorderRadius.all(Radius.circular(10)),
										color: Color.fromARGB(255, 240, 240, 240),
									),
								),
								positiveText: "好的",
								onPositivePressEvent: (){
									Navigator.pop(context);
								},
								onCloseEvent: (){
									Navigator.pop(context);
								},
							);
						}
				);
			}else if(resTypeEntity.type == 3){
				showDialog(context: context,barrierDismissible: true,
						builder: (_){
							return MessageDialog(
								imageAsset: "assets/images/wet_icon.png",
								title: "分类信息",
								content: Container(
									alignment: Alignment.center,
									padding: EdgeInsets.only(left: 10,right: 10),
									child: Text("属于湿垃圾", style: TextStyle(fontSize: 17, color: Color.fromARGB(255, 75, 75, 75)),),
									decoration: BoxDecoration(
										borderRadius: BorderRadius.all(Radius.circular(10)),
										color: Color.fromARGB(255, 240, 240, 240),
									),
								),
								positiveText: "好的",
								onPositivePressEvent: (){
									Navigator.pop(context);
								},
								onCloseEvent: (){
									Navigator.pop(context);
								},
							);
						}
				);
			}



		}
	}
}